﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace NGE
{
    public class Inputer : NativeWindow
    {
        Game game;
        public Inputer(Game g)
        {
            game = g;
            AssignHandle(game.GameControl.Handle);
            //HideTitleBar();
        }       
        //InputLanguage.

        #region const var define
        const int WM_GETDLGCODE = 0x87;
        const int WM_IME_SETCONTEXT = 0x0281;
        const int WM_INPUTLANGCHANGE = 0x0051;
        const int WM_IME_ENDCOMPOSITION = 0x10E;
        const int WM_CHAR = 0x102;
        const int WM_CREATE = 0x1;

        const int WM_NCHITTEST = 0x84;
        const int WM_NCLBUTTONDBLCLK = 0x00A3;
        const int WM_NCMOUSELEAVE = 0x2A2;

        const int WS_CHILD = 0x40000000;
        const int WS_VISIBLE = 0x10000000;

        const int HTCLIENT = 0x1;
        const int HTCAPTION = 0x2;

        const int DLGC_WANTARROWS = 0x0001;/* Control wants arrow keys         */
        const int DLGC_WANTTAB = 0x0002;/* Control wants tab keys           */
        const int DLGC_WANTALLKEYS = 0x0004;/* Control wants all keys           */
        const int DLGC_WANTMESSAGE = 0x0004;/* Pass message to control          */
        const int DLGC_HASSETSEL = 0x0008;/* Understands EM_SETSEL message    */
        const int DLGC_DEFPUSHBUTTON = 0x0010;/* Default pushbutton               */
        const int DLGC_UNDEFPUSHBUTTON = 0x0020;/* Non-default pushbutton           */
        const int DLGC_RADIOBUTTON = 0x0040;/* Radio button                     */
        const int DLGC_WANTCHARS = 0x0080;/* Want WM_CHAR messages            */
        const int DLGC_STATIC = 0x0100;/* Static item: don't include       */
        const int DLGC_BUTTON = 0x2000;/* Button item: can be checked      */

        const int GCS_COMPREADSTR = 0x0001;
        const int GCS_COMPREADATTR = 0x0002;
        const int GCS_COMPREADCLAUSE = 0x0004;
        const int GCS_COMPSTR = 0x0008;
        const int GCS_COMPATTR = 0x0010;
        const int GCS_COMPCLAUSE = 0x0020;
        const int GCS_CURSORPOS = 0x0080;
        const int GCS_DELTASTART = 0x0100;
        const int GCS_RESULTREADSTR = 0x0200;
        const int GCS_RESULTREADCLAUSE = 0x0400;
        const int GCS_RESULTSTR = 0x0800;
        const int GCS_RESULTCLAUSE = 0x1000;
        #endregion
        int Msg, WParam, LParam;
        IntPtr IMC;
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);           
            
            this.Msg = m.Msg;
            this.WParam = m.WParam.ToInt32();
            this.LParam = m.LParam.ToInt32();

            //Keys.ResetAll();

            #region 输入法转换
            //if (Msg == WM_INPUTLANGCHANGE)
            //{
            //    //string IMCName = GetInputLanguageName(m.LParam);
            //    //metalhunter.consoleComponent.AppendMessageLog("！本地：", IMCName);
            //    //WM.sys.ShowDialog();
            //    return;
            //}
            if (Msg == WM_IME_SETCONTEXT)
            {
                if (IMC == IntPtr.Zero)
                {
                    IMC = WindowsAPI.ImmGetContext(this.Handle);
                }
                WindowsAPI.ImmAssociateContext(this.Handle, IMC);
            }
            #endregion

            #region 输入法输入
            if (Msg == WM_GETDLGCODE)
            {
                if (IntPtr.Size == 4)
                {
                    int result = m.Result.ToInt32();
                    result |= DLGC_WANTALLKEYS;
                    result |= DLGC_WANTCHARS;
                    m.Result = new IntPtr(result);
                }
            }
            /*
            else if (Msg == WindowsAPI.WM_IME_ENDCOMPOSITION)
            {
                if (metalhunter.consoleComponent.InputBoxVisible)
                {
                    metalhunter.consoleComponent.SetInputMessage(GetInputString());
                }
                return;
                //metalhunter.consoleComponent.MessageBox(GetInputString(), 3);
            }*/
            if (Msg == WM_CHAR)
            {
                string txt;
                int charbuf = m.WParam.ToInt32();
                foreach (Component com in game.Components)
                {
                    byte[] buf = BitConverter.GetBytes(charbuf);
                    txt = Encoding.Unicode.GetString(buf);
                    for (int i = 0; i < txt.Length; i++)
                    {
                        string chara = txt.Substring(i, 1);
                        if (chara != "\0")
                        {
                            com.NotifyInputerEvent(chara);
                        }
                    }
                }


                //if (non_response > 0)
                //{
                //    non_response--;
                //}
                //else
                //{
                //    int charbuf = m.WParam.ToInt32();
                //    foreach (Component com in game.Components)
                //    {
                //        string txt;
                //        if (charbuf <= 13)
                //        {
                //            byte[] buf = BitConverter.GetBytes((short)charbuf);
                //            txt = Encoding.Unicode.GetString(buf);
                //        }
                //        else
                //        {
                //            txt = inputText;
                //            non_response = inputText.Length - 1;
                //        }
                //        com.NotifyInputerEvent(txt);
                //    }
                //}
                //if (metalhunter.consoleComponent.InputBoxVisible)
                //{
                //    if (charbuf == 0x8)
                //    {
                //        string buf = metalhunter.consoleComponent.InputMessage;
                //        if (buf.Length > 0)
                //        {
                //            buf = buf.Substring(0, buf.Length - 1);
                //            metalhunter.consoleComponent.SetInputMessage(buf);
                //        }
                //    }
                //    else if (charbuf == 0xd)
                //    {
                //        metalhunter.consoleComponent.MyIMC = InputLanguage.CurrentInputLanguage.LayoutName;
                //        InputLanguage.CurrentInputLanguage = InputLanguage.DefaultInputLanguage;
                //    }
                //    else if (charbuf == 0x1b)
                //    {
                //        InputLanguage.CurrentInputLanguage = InputLanguage.DefaultInputLanguage;
                //        metalhunter.consoleComponent.InputMessage = "";
                //        metalhunter.consoleComponent.InputBoxVisible = false;
                //    }
                //    else
                //    {
                //        metalhunter.consoleComponent.AppendInputMessage(new string((char)charbuf, 1));
                //    }
                //}
            }
            #endregion
            ////////////////////////////////////////////////////////////////////////////////////////////
            #region 无标题栏拖拽窗口
            //if (m.Msg == WM_NCLBUTTONDBLCLK)
            //{
            //    return;
            //}
            //if (m.Msg == WM_NCHITTEST)
            //{
            //    if (game.GameControl is Form)
            //    {
            //        if (m.Result == (IntPtr)HTCLIENT)
            //        {
            //            m.HWnd = this.Handle;
            //            m.Result = (IntPtr)HTCAPTION;
            //        }
            //        return;
            //    }
            //}
            #endregion
            #region 按键处理
            //if (Msg == KeyState.IsDown)
            //{
            //    //Keys.SetKeyState(WParam, Msg);
            //}
            //if (Msg == KeyState.IsUp)
            //{
            //    if (WParam == 0x4e)
            //    {
            //        if (metalhunter.consoleComponent.InputBoxVisible == false)
            //        {
            //            metalhunter.LockAllComponent();
            //            LoginMgr loginmgr = new LoginMgr();
            //            loginmgr.Show();
            //            //metalhunter.UnlockAllComponent();
            //        }
            //    }
            //    else if (WParam == 0x54)
            //    {
            //        if (metalhunter.consoleComponent.InputBoxVisible == false)
            //        {
            //            metalhunter.LockAllComponent();
            //            TeamMgr teammgr = new TeamMgr();
            //            teammgr.Show();
            //        }
            //    }
            //    else
            //    {
            //        Keys.SetKeyState(WParam, Msg);
            //    }
            //}
            #endregion
            ////////////////////////////////////////////////////////////////////////////////////////////
            //metalhunter.consoleComponent.MessageBox(m.ToString(), 3);
        }

        //void HideTitleBar()
        //{
        //    int style = WindowsAPI.GetWindowLong(this.Handle, -16);
        //    style &= ~0x00C00000;
        //    WindowsAPI.SetWindowLong(this.Handle, -16, style);
        //}

        //void loginmgr_Shown(object sender, EventArgs e)
        //{
        //    WindowsAPI.SetWindowLong(((Form)sender).Handle, -20, 0x00080000);
        //    WindowsAPI.SetLayeredWindowAttributes(((Form)sender).Handle, 0, 155, 2);
        //    //throw new NotImplementedException();
        //}
        //int non_response;
        //string inputedText;
        //string inputText
        //{
        //    get
        //    {
        //        int length = WindowsAPI.ImmGetCompositionString(WindowsAPI.ImmGetContext(this.Handle), GCS_RESULTSTR, null, 0);
        //        byte[] buff = new byte[length];
        //        WindowsAPI.ImmGetCompositionString(WindowsAPI.ImmGetContext(this.Handle), GCS_RESULTSTR, buff, buff.Length);
        //        return Encoding.Default.GetString(buff);
        //    }
        //}
        //public void set()
        //{
        //    InputLanguage.CurrentInputLanguage = InputLanguage.InstalledInputLanguages[1];
        //}
        public string InputLanguageName
        {
            get
            {
                //StringBuilder inputLanguageName = new StringBuilder(128);
                //WindowsAPI.ImmGetDescription(IMC, inputLanguageName, inputLanguageName.Capacity);
                //return inputLanguageName.ToString();
                return InputLanguage.CurrentInputLanguage.LayoutName;
            }
        }
        InputLanguage UserInputLanguage;
        public void OpenInputLanguage()
        {
            InputLanguage.CurrentInputLanguage = UserInputLanguage;
        }
        public void CloseInputLanguage()
        {
            UserInputLanguage = InputLanguage.CurrentInputLanguage;
            InputLanguage.CurrentInputLanguage = InputLanguage.DefaultInputLanguage;
        }
    }
}
